//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.wi.list;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

public class ListGUI extends JInternalFrame {

    private class TableModel extends AbstractTableModel {

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public boolean isCellEditable(int row, int col) {
            return false;
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }

        TableModel() {
        }
    }

    public ListGUI(String title, String iconName) {
        alternateColor = new Color(210, 230, 250);
        setFrameIcon(new ImageIcon(getClass().getResource("/org/jpgolay/wi/images/" + iconName)));
        initComponents();
    }

    public ListGUI(String iconName) {
        this("Liste", iconName);
    }

    private void initComponents() {
        searchP = new JPanel();
        searchFieldP = new JPanel();
        selectCB = new JComboBox();
        searchF = new JTextField();
        searchB = new JButton();
        listP = new JPanel();
        listSP = new JScrollPane();
        infoP = new JPanel();
        infoLeftL = new JLabel();
        infoRightL = new JLabel();
        list = new JTable(new TableModel()) {
            public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) {
                Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
                if (rowIndex % 2 == 0) {
                    c.setBackground(alternateColor);
                } else {
                    c.setBackground(getBackground());
                }
                return c;
            }
        };
        list.setShowHorizontalLines(false);
        searchFieldP.setLayout(new AbsoluteLayout());
        searchFieldP.add(selectCB, new AbsoluteConstraints(0, 0, 150, 25));
        searchF.setHorizontalAlignment(0);
        searchFieldP.add(searchF, new AbsoluteConstraints(150, 0, 150, 25));
        searchB.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/wi/images/loupe.png")));
        searchB.setPreferredSize(new Dimension(50, 22));
        searchB.setContentAreaFilled(false);
        searchB.setBorderPainted(false);
        searchB.setFocusPainted(false);
        searchB.setToolTipText("Lancer la recherche");
        searchFieldP.add(searchB, new AbsoluteConstraints(300, 0, 100, 25));
        searchP.add(searchFieldP);
        getContentPane().add(searchP, "North");
        listP.setLayout(new BorderLayout());
        listSP.setViewportView(list);
        listP.add(listSP, "Center");
        getContentPane().add(listP, "Center");
        infoP.setLayout(new GridLayout());
        infoP.setBorder(new BevelBorder(1));
        infoLeftL.setHorizontalAlignment(2);
        infoLeftL.setText("  ");
        infoP.add(infoLeftL);
        infoRightL.setHorizontalAlignment(4);
        infoRightL.setText("  ");
        infoP.add(infoRightL);
        getContentPane().add(infoP, "South");
        pack();
    }

    public JTable getList() {
        return list;
    }

    public void setList(JTable contenuT) {
        list = contenuT;
    }

    public String getInfoLeft() {
        return infoLeftL.getText();
    }

    public void setInfoLeft(String infoLeft) {
        infoLeftL.setText(infoLeft);
    }

    public String getInfoRight() {
        return infoRightL.getText();
    }

    public void setInfoRight(String infoRight) {
        infoRightL.setText(infoRight);
    }

    public JTextField getSearchF() {
        return searchF;
    }

    public String getSearch() {
        return searchF.getText();
    }

    public void setSearch(String search) {
        searchF.setText(search);
    }

    public JComboBox getSelectCB() {
        return selectCB;
    }

    public String getSelectItem() {
        return (String) selectCB.getSelectedItem();
    }

    public void setSelectItem(String select) {
        selectCB.setSelectedItem(select);
    }

    public int getSelectIndex() {
        return selectCB.getSelectedIndex();
    }

    public void setSelectIndex(int select) {
        selectCB.setSelectedIndex(select);
    }

    public void setSelectModel(String model[]) {
        selectCB.setModel(new DefaultComboBoxModel(model));
    }

    public JButton getSearchB() {
        return searchB;
    }

    public String[] getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(String columnNames[]) {
        this.columnNames = columnNames;
    }

    public Object[][] getData() {
        return data;
    }

    public void setData(Object data[][]) {
        this.data = data;
        list.setModel(new TableModel());
        listSP.setViewportView(list);
    }
    private JPanel listP;
    private JScrollPane listSP;
    private JTable list;
    private JLabel infoLeftL;
    private JPanel infoP;
    private JLabel infoRightL;
    private JButton searchB;
    private JTextField searchF;
    private JPanel searchFieldP;
    private JPanel searchP;
    private JComboBox selectCB;
    private Color alternateColor;
    private Object data[][] = {
        {
            "", "", "", ""
        }, {
            "", "", "", ""
        }, {
            "", "", "", ""
        }, {
            "", "", "", ""
        }, {
            "", "", "", ""
        }
    };
    private String columnNames[] = {
        "", "", "", ""
    };
}
